using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface ICreditsMenu
  {
    event MenuEvent OnCBackClicked;
  }

  public class CreditsMenu : BaseMenu, ICreditsMenu
  {

    Button BackToMM;
    Vector2 savedPos;
    public bool Animated = false;
    public int Direction = 1;
    public bool credit = false;
    Color color2 = Color.White;
    float skySplash = 15f;
    SpriteBatch spriteBatch;
    Color color;
    int scrollY = 480, scrollX = 0, AnimVelocity = 60;
    
    public CreditsMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = game.Content.Load<Texture2D>(@"Images//Menu//CreditsCard");
      bounds = new Rectangle(70, 10, 220, 331);
      game.Services.AddService(typeof(ICreditsMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(ICreditsMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      savedPos.X = bounds.X;

      color = Color.Black;
      color.A = 0;

      
      BackToMM = new Button(Game, @"Images//Menu//button2", new Rectangle(600, 400, 120, 55), Color.Transparent, "small_font", Locale.Strings.Back, Color.Transparent);
      AddButton(BackToMM, () =>
      {
        BackToMM.touched = true;
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
      });

      base.Initialize();
    }

    int counter = 4;

    public override void Update(GameTime gameTime)
    {

      if (BackToMM.touched)
      {
        counter--;
        if(counter < 0)
        {
          Animated = true;
          credit = true;
        }
      
      }

      if (menuDepth == MenuDepths.ForeGround)
      {
        BackToMM.color = Color.MediumBlue;
        BackToMM.textColor = Color.White;
      }

      // Input: Back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        Animated = true;
        credit = true;
      }
      
      if (Animated)
      {
        bounds.X += Direction * AnimVelocity;
               
        
        if (bounds.X > (savedPos.X + bounds.Width + 30))
        {
          
          Direction = -1;
          if (menuDepth == MenuDepths.BackMenu)
          {
            menuDepth = MenuDepths.ForeGround;
            DrawOrder = (int)menuDepth;            
          }
          else
          {            
            menuDepth = MenuDepths.BackMenu;
            DrawOrder = (int)menuDepth;
          }
        }

        if (bounds.X < savedPos.X)
        {
          int offset = (int)savedPos.X - bounds.X;
          Animated = false;
          Direction = 1;
          bounds.X = (int)savedPos.X;
          
          foreach (Button b in (from cb in buttons select cb.b))
          {
            if (!b.message.Equals(Locale.Strings.Back))
              b.bounds.X += Direction * offset;
          }

          if (menuDepth == MenuDepths.BackMenu)
          {
            if (OnCBackClicked != null)
              OnCBackClicked();
          }

        }

      }

     if ((menuDepth == MenuDepths.ForeGround)  || (credit))
      { 
        
        if ((color.A < 230) && (!credit))        
          color.A += 10;

       if(credit)
         color.A -= 10;
                    
      }


      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {

      if (menuDepth == MenuDepths.ForeGround)
      {
        spriteBatch.Begin();

        if ((bounds.X >= 240) && (credit))
          scrollX += 60;

        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"white_pixel"), new Rectangle(0, 0, 800, 480), color);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Coordinator, new Vector2(310 + scrollX, bounds.Y + scrollY), Color.Red);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.CreditsCoordinator, new Vector2(330 + scrollX, bounds.Y + scrollY + 40), Color.White);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Programmers, new Vector2(310 + scrollX, bounds.Y + scrollY + 100), Color.Red);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.CreditsProgrammers.Replace("\\n", "\n"), new Vector2(330 + scrollX, bounds.Y + scrollY + 140), Color.White);
        //spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Graphics, new Vector2(310 + scrollX, bounds.Y + scrollY + 255), Color.Red);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.SpecialThanks, new Vector2(310 + scrollX, bounds.Y + scrollY + 280), Color.Red);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.CreditsGraphics.Replace("\\n", "\n"), new Vector2(330 + scrollX, bounds.Y + scrollY + 320), Color.White);        
        //spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Graphics.Replace("\\n", "\n"), new Vector2(330 + scrollX, bounds.Y + scrollY + 295), Color.White);
        spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), Locale.Strings.TeamEmail.Replace("\\n", "\n"), new Vector2(20, bounds.Y + 380), Color.Red, 0, Vector2.Zero, skySplash,SpriteEffects.None, 0);

        
        scrollY -= 2;
        if (scrollY < -500)
          scrollY = 500;

        if(skySplash > 1f)
          skySplash -= 0.3f;


        spriteBatch.End();

      }
      base.Draw(gameTime);
        
      }



    public event MenuEvent OnCBackClicked;    
  }
}
